
// AF2 Distance to School Map
// AF2 Distance to School Map
// AF2 Distance to School Map


// PRELIMINARIES

clear
set more off
set scheme s1mono

// LOAD DATA

use "JakielaOzierFernaldKnauer-preschool-data.dta", replace

cd "tmp"


// MAKE BULLSEYE FIGURE SUMMARIZING IDENTIFICATION STRATEGY

** Terciles of distance to the school
** Approx cutoffs:  360m, 540m (all schools<750m)

gen distcell = cond(schdist<0.36,1,cond(schdist<0.54,2,3))
bys distcell:  egen meancell = mean(inschool)

** generate (x,y) pairs of relative distance (to the school)


replace normlat = 111111*normlat // convert to meters
replace normlong = 111111*normlong // convert to meters

** create a scatter point for the school at (0,0)

gen schoollat = 0 in 1
gen schoollong = 0 in 1

** generate x variable that spans the relevant range in meters (-750,+750)

gen x = _n
replace x = -800 + ((x-1)*1600)/2502

** define the arcs that we'll use to create the bullseye
gen c360pos = sqrt(360^2-x^2) if abs(x)<=360
gen c540pos = sqrt(540^2-x^2) if abs(x)<=540
gen c750pos = sqrt(775^2-x^2) if abs(x)<=775
gen c360neg = - sqrt(360^2-x^2) if abs(x)<=360
gen c540neg = - sqrt(540^2-x^2) if abs(x)<=540
gen c750neg = - sqrt(775^2-x^2) if abs(x)<=775

** graph for 3 year olds

local thisage = 3

local bullcolor "lavender"
local kidcolor "dknavy"

sum inschool if distcell==1 & roundage==`thisage'
local i1 = 0.10+(r(mean)-0.5)*(0.8/0.5) // scale up range of color intensities to show variation among 3 year olds
sum inschool if distcell==2 & roundage==`thisage'
local i2 = 0.10+(r(mean)-0.5)*(0.8/0.5)
sum inschool if distcell==3 & roundage==`thisage'
local i3 = 0.10+(r(mean)-0.5)*(0.8/0.5)

twoway ///
 (rarea c750pos c750neg x, aspect(1) color(`bullcolor'*`i3') lcolor(white)) ///
 (rarea c540pos c540neg x, color(`bullcolor'*`i2') lcolor(white)) ///
 (rarea c360pos c360neg x, color(`bullcolor'*`i1') lcolor(white)) ///
 (scatter normlat normlong if inschool==1 & roundage==`thisage', mcolor(`kidcolor'*0.72) msymbol(oh) msize(medsmall)) ///
 (scatter normlat normlong if inschool==0 & roundage==`thisage', mcolor(`kidcolor') msymbol(th)) ///
 (scatter schoollat schoollong, mcolor(gs4) msymbol(S) msize(medium)) , ///
 legend(cols(1) order(3 2 1 4 5 6) size(small) /// 
 label(1 "Distance tercile 3:  51% enrolled") ///
 label(2 "Distance tercile 2:  58% enrolled") ///
 label(3 "Distance tercile 1:  72% enrolled") ///
 label(4 "3 year old in school") ///
 label(5 "3 year old not in school") ///
 label(6 "Primary school")) ///
 title("School Enrollment among 3 Year Olds", size(small)) ///
 xla(6(12)6,nogrid) yla(6(12)6,nogrid) ysc(off) xsc(off) graphr(fc(white)) plotr(style(none)) saving(three, replace)
 
** graph for 4 year olds

local thisage = 4


sum inschool if distcell==1 & roundage==`thisage'
local i1 = 0.10+(r(mean)-0.5)*(0.8/0.5) // scale up range of color intensities to show variation among 3 year olds
sum inschool if distcell==2 & roundage==`thisage'
local i2 = 0.10+(r(mean)-0.5)*(0.8/0.5)
sum inschool if distcell==3 & roundage==`thisage'
local i3 = 0.10+(r(mean)-0.5)*(0.8/0.5)

twoway ///
 (rarea c750pos c750neg x, aspect(1) color(`bullcolor'*`i3') lcolor(white)) ///
 (rarea c540pos c540neg x, color(`bullcolor'*`i2') lcolor(white)) ///
 (rarea c360pos c360neg x, color(`bullcolor'*`i1') lcolor(white)) ///
 (scatter normlat normlong if inschool==1 & roundage==`thisage', mcolor(`kidcolor'*0.72) msymbol(oh) msize(medsmall)) ///
 (scatter normlat normlong if inschool==0 & roundage==`thisage', mcolor(`kidcolor') msymbol(th)) ///
 (scatter schoollat schoollong, mcolor(gs4) msymbol(S) msize(medium)) , ///
 legend(cols(1) order(3 2 1 4 5 6) size(small) /// 
 label(1 "Distance tercile 3:  87% enrolled") ///
 label(2 "Distance tercile 2:  87% enrolled") ///
 label(3 "Distance tercile 1:  86% enrolled") ///
 label(4 "4 year old in school") ///
 label(5 "4 year old not in school") ///
 label(6 "Primary school")) ///
 title("School Enrollment among 4 Year Olds", size(small)) ///
 xla(6(12)6,nogrid) yla(6(12)6,nogrid) ysc(off) xsc(off) graphr(fc(white)) plotr(style(none)) saving(six, replace)
 
graph combine three.gph six.gph
cd ..
graph export "out/AF2-id-bullseye.pdf", replace

